package com.example.home

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.home.adapter.GlideAdapter
import com.example.home.adapter.TabAdapter
import com.example.home.adapter.VideoAdapter
import com.example.home.entity.TabItem
import com.example.home.entity.VideoIdEntity


class HomeFragment : Fragment() {
    private var notificationBadge: TextView? = null
    private lateinit var tabRecyclerView: RecyclerView
    private lateinit var videoRecyclerView: RecyclerView
    private lateinit var glideRecyclerView: RecyclerView
    private var type = "关注"

    // 使用 Hilt 注入 ViewModel
//        private val homeViewModel: HomeViewModel by viewModels()
    private val tabList = mutableListOf(
        TabItem("关注", true),
        TabItem("推荐", false),
        TabItem("Vlog", false),
        TabItem("影视", false),
        TabItem("娱乐", false),
        TabItem("体育", false),
        TabItem("NBA", false)
    )
    private val videoList = mutableListOf(
        VideoIdEntity(
            avatar_url = "https://sf6-cdn-tos.bdxiguastatic.com/img/user-avatar/0356cfffdc5f519188be826da4fff737~300x300.image",
            channelid = "94349546845",
            commentnum = 0,
            ctime = "2025-09-18T02:43:35.000+00:00",
            description = "吃土少女popmatta",
            group_id = "7034821008162816520",
            id = 1471,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0004/05228f8b076e47c59e7c90e20e68b81b~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386683&x-signature=ckA7vbLXDUMunKn8JZmmGvE02ag%3D",
            item_id = "7034821008162816520",
            labelIds = "6,2,2",
            name = "马佳Matta",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/9c176de6c8be29b5730759eb262f9f4a/6256c74e/video/tos/cn/tos-cn-v-0000c2429/1819f16e13414268a3c1505a50f0ac6c/?a=1768&br=905&bt=905&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThW~Rjg1t8Z&l=20220413195122010210018",
            publish_time = "null",
            title = "外国人来中国以后，才明白什么叫大城市",
            userid = "3548825314134611",
            verifycode = "a92e777fbc2793306510a40fa9f99b02",
            videomainimag = "http://js2.a.yximgs.com/upic/2020/08/22/18/BMjAyMDA4MjIxODA4MzVfMTk3MzcyNDk5Nl8zNDgwNTA5MDM0Ml8xXzM=_low_Bebef85a20dcc5d1b4e9c314dcaf00865.webp?tag=1-1599270526-vdg-0-rtqvpgwhr8-3de7643c122e9a1a&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/08/22/18/BMjAyMDA4MjIxODA4MzVfMTk3MzcyNDk5Nl8zNDgwNTA5MDM0Ml8xXzM=_b_B6d8a675a71f378618d2fedd74da1ebf1.mp4?tag=1-1599270526-vdg-1-2qndbafkcd-c6e62465358a725f&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf3-cdn-tos.bdxiguastatic.com/img/user-avatar/5e247074cc41ded1105a8825734afb5a~300x300.image",
            channelid = "94349546845",
            commentnum = 0,
            ctime = "2025-09-18T02:30:26.000+00:00",
            description = "独立摄影师",
            group_id = "7077435445931409956",
            id = 1484,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0004/e31d80d7782d456ca405b60e6d5b2bb8~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386691&x-signature=CNgKR8n1bUKazMMAh%2BdcBiyktwU%3D",
            item_id = "7077435445931409956",
            labelIds = "2,7,10",
            name = "行者沐然",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/d33cbdd5ccf158ff798cb2e7a5bd2d21/6256c756/video/tos/cn/tos-cn-v-0000c2429/f079ee45e4f44252a689edbe27afbc61/?a=1768&br=755&bt=755&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThWK8jg1t8Z&l=20220413195130010212138",
            publish_time = "null",
            title = "在塞尔维亚的华人因为这座城市发财了，什么地方？怎么回事儿？",
            userid = "6707503620",
            verifycode = "98d867a864b3c2277008e458942b50c6",
            videomainimag = "http://js2.a.yximgs.com/upic/2020/08/29/19/BMjAyMDA4MjkxOTU1MjJfMzI2MzgyMDNfMzUyMzE5MDc4MDdfMV8z_low_B55720a1b6c21394d199661eb3f61b507.webp?tag=1-1599270528-vdg-0-u2rwepupvt-8362bb49c0d22cee&bp=10000",
            videopath = "http://alimov2.a.yximgs.com/upic/2020/08/29/19/BMjAyMDA4MjkxOTU1MjJfMzI2MzgyMDNfMzUyMzE5MDc4MDdfMV8z_b_Bd431e9d66f33d75a0653c4e46a766767.mp4?tag=1-1599270528-vdg-1-vmnyp4xhx5-32907a970fac884f&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf1-cdn-tos.bdxiguastatic.com/img/user-avatar/9a00ec56042418fa72a8fa37a09a796d~300x300.image",
            channelid = "94349546845",
            commentnum = 0,
            ctime = "2025-09-18T02:30:22.000+00:00",
            description = "FIBO咖啡创始人",
            group_id = "6881449844381057549",
            id = 1494,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0004/5dc7e2622a634f58985c1ba0b15026d2~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386698&x-signature=YzzHJN4lG6m57srQR%2FqNWDOX32w%3D",
            item_id = "6881449844381057549",
            labelIds = "7,3,2",
            name = "我是郭杰瑞",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/7b19d2307826a6c40bf2d5ce2fce9780/6256c75c/video/tos/cn/tos-cn-v-0000/3ef9ead617de4b89afa3379e8f903c62/?a=1768&br=921&bt=921&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThWv8jg1t8Z&l=2022041319513701015803608604",
            publish_time = "null",
            title = "入住美国移动房屋，几千美元买栋可以拖走的房子值不值？",
            userid = "60140020874",
            verifycode = "8afcbd13010fcbc784ad02c33d285ec0",
            videomainimag = "http://ali2.a.yximgs.com/upic/2020/09/04/10/BMjAyMDA5MDQxMDAwMjhfMzQwMDY4NDY0XzM1NDkzOTM1NTM1XzJfMw==_low_Bab82f212fce564907add11d18720e9c1.webp?tag=1-1599270592-vdg-0-iab5xe9zhb-b53e4aefaf134c59&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/09/04/10/BMjAyMDA5MDQxMDAwMjhfMzQwMDY4NDY0XzM1NDkzOTM1NTM1XzJfMw==_b_Bab4b9a5309faf4335a7d129207da52e6.mp4?tag=1-1599270592-vdg-1-ehpj13szlm-1ea4d35e25872f18&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf6-cdn-tos.bdxiguastatic.com/img/pgc-image/bdcb67f5a175418ea9cc1b961d6cc85f~300x300.image",
            channelid = "94349546845",
            commentnum = 0,
            ctime = "2025-06-04T02:13:03.000+00:00",
            description = "想把生活拍成电影",
            group_id = "6972365459303858722",
            id = 1473,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0026/c93a4b2624b241b0ab37b64ce0931b9e~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386683&x-signature=adIUcT7rtQ%2FH0fdvpljpI6p34SE%3D",
            item_id = "6972365459303858722",
            labelIds = "6,2,10",
            name = "俊晖JAN",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/3603bf825b63d70fa2bae077e3eeb6c0/6256c74d/video/tos/cn/tos-cn-v-0000c002/81f506dff2614c5a88390602a45abb92/?a=1768&br=1579&bt=1579&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThW~Rjg1t8Z&l=2022041319512201021001",
            publish_time = "null",
            title = "一个背了两年的包里都装了什么好东西？",
            userid = "102373059972",
            verifycode = "80f10ce3c71cacdbee0b528cf97a6de0",
            videomainimag = "http://ali2.a.yximgs.com/upic/2020/09/01/18/BMjAyMDA5MDExODExMjFfMTAzMTMxMDIyXzM1MzgzNDI3NzU4XzFfMw==_low_B0771b493b3a9a16ccd5adb9fca436f65.webp?tag=1-1599270526-vdg-0-vul073xzts-df7ab8fb47d6b373&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/09/01/18/BMjAyMDA5MDExODExMjFfMTAzMTMxMDIyXzM1MzgzNDI3NzU4XzFfMw==_b_B73631ee39222a251ec8ffe8ee653298c.mp4?tag=1-1599270526-vdg-1-qwsl47fo8z-15869b7660b1449a&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/733d41b8d6424992a9d446a017153447~300x300.image",
            channelid = "94349546885",
            commentnum = 0,
            ctime = "2025-08-10T12:08:52.000+00:00",
            description = "最全的舞蹈视频、教学、学习技巧。",
            group_id = "6907910140293448206",
            id = 1514,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0004/3287be14f99147e9a10eff23fb863590~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386713&x-signature=HxPN%2BxN6nsfKG8EfAXbGgpa2eUk%3D",
            item_id = "6907910140293448206",
            labelIds = "5,2,6",
            name = "单色舞蹈",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/655325a5f0a9cae4ba5b957fc5e5e504/6256c76b/video/tos/cn/tos-cn-v-0000/dc8c9b6039aa4f6faaa3b3537c3aae92/?a=1768&br=929&bt=929&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThWt8jg1t8Z&l=2022041319515201021117409206",
            publish_time = "null",
            title = "舞蹈生的一堂课，远没有你想象的那么简单！",
            userid = "6826359332",
            verifycode = "c70ca6d4d9f3b6a8a5e41f2ba5384b3e",
            videomainimag = "http://ali2.a.yximgs.com/upic/2020/08/24/10/BMjAyMDA4MjQxMDIwMDlfOTAyMjc0OTQwXzM0ODk4MjA3NzgwXzFfMw==_low_B43829c3deb5bbc9d0ed8086382863ce0.webp?tag=1-1599270652-vdg-0-szmzz86yjt-5a04db8d5e4e36f9&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/08/24/10/BMjAyMDA4MjQxMDIwMDlfOTAyMjc0OTQwXzM0ODk4MjA3NzgwXzFfMw==_b_B895052065604cd3bb2b887d8a2863a1b.mp4?tag=1-1599270652-vdg-1-utgk1vu7kw-b62db480d0e6973e&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf1-cdn-tos.bdxiguastatic.com/img/user-avatar/6fec6244ff5982352e31eb00aa23713d~300x300.image",
            channelid = "94349547160",
            commentnum = 0,
            ctime = "2025-08-10T12:15:12.000+00:00",
            description = "欢迎关注NBA官方头条号",
            group_id = "6982025218428666381",
            id = 1757,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0004/1f1b4d46b88c4cc9a92ce9f9c34dcc90~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386920&x-signature=yGbWKDbHOZm1qLJFiBODEUrMOUc%3D",
            item_id = "6982025218428666381",
            labelIds = "2,5,3",
            name = "NBA",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/c05b8bcadc5144216ff4036318142d2a/6256c83b/video/tos/cn/tos-cn-v-0000c002/bfe57d4686234af09a8162d8edf8a7bf/?a=1768&br=1225&bt=1225&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThWbLjg1t8Z&l=2022041319551901015120",
            publish_time = "null",
            title = "NBA集锦：单节16分！保罗总决赛首秀表现亮眼砍32+4+9率队取胜",
            userid = "85383446500",
            verifycode = "10d2ea10bf5d73844feb9ecb84fdf356",
            videomainimag = "http://ali2.a.yximgs.com/upic/2020/09/04/18/BMjAyMDA5MDQxODA5MDVfMTY1NTM5NTIwM18zNTUxMjkzNDI1N18xXzM=_low_Bb93e68f940ae95e3b77375ca627ada43.webp?tag=1-1599271261-vdg-0-t5cxdh0dzu-779c45af79054bd5&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/09/04/18/BMjAyMDA5MDQxODA5MDVfMTY1NTM5NTIwM18zNTUxMjkzNDI1N18xXzM=_b_B04f7c8632d6ca3c893ef628aeb4e97c8.mp4?tag=1-1599271261-vdg-1-xxivcwxwtf-0197c23808d52992&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf1-cdn-tos.bdxiguastatic.com/img/user-avatar/6fec6244ff5982352e31eb00aa23713d~300x300.image",
            channelid = "94349547160",
            commentnum = 0,
            ctime = "2025-06-04T02:13:03.000+00:00",
            description = "欢迎关注NBA官方头条号",
            group_id = "6973501619384091172",
            id = 1751,
            image_url = "http://p26-sign.bdxiguaimg.com/tos-cn-i-0004/474754e2054d487a91695b78816884f7~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386920&x-signature=kURA68c4W8u3XGtg9Axxpo7Xc7w%3D",
            item_id = "6973501619384091172",
            labelIds = "10,9,2",
            name = "NBA",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/10786d40530db348d65c937f3577d14b/6256c83b/video/tos/cn/tos-cn-v-0000/57bdde3dd8254086855497ef5a5a8a9f/?a=1768&br=1277&bt=1277&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThWbLjg1t8Z&l=20220413195519010151201072",
            publish_time = "null",
            title = "NBA今日扣篮时刻：戈登抢断暴扣一条龙 杜兰特独闯龙潭怒扣终结！",
            userid = "85383446500",
            verifycode = "0c8d8337f4b2f5b536d67be96914409c",
            videomainimag = "http://ali2.a.yximgs.com/upic/2020/08/28/19/BMjAyMDA4MjgxOTIzMjBfMTE0Nzk4OTA2XzM1MTcyMzY2MzQwXzFfMw==_low_B333a5ac2fc0641df334ca7d59e929f97.webp?tag=1-1599271261-vdg-0-vgr8gui3t2-39f25c501d7e6fe7&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/08/28/19/BMjAyMDA4MjgxOTIzMjBfMTE0Nzk4OTA2XzM1MTcyMzY2MzQwXzFfMw==_b_B1b83f0d68aa886fb39e357cd7cc68583.mp4?tag=1-1599271261-vdg-1-3vxczuzfu0-eb3671f877547d3c&tt=b&bp=10000"
        ),
        VideoIdEntity(
            avatar_url = "https://sf1-cdn-tos.bdxiguastatic.com/img/user-avatar/6fec6244ff5982352e31eb00aa23713d~300x300.image",
            channelid = "94349547160",
            commentnum = 0,
            ctime = "2025-07-02T10:49:43.000+00:00",
            description = "欢迎关注NBA官方头条号",
            group_id = "6971269651795804710",
            id = 1750,
            image_url = "http://p6-sign.bdxiguaimg.com/tos-cn-i-0004/4a73c59b66b54c74ab70cce7fceafd40~tplv-pk90l89vgd-crop-center:864:486.avif?x-expires=1681386920&x-signature=JjMrw9%2BGlraCYpO6cpDhGKMPFXc%3D",
            item_id = "6971269651795804710",
            labelIds = "3,8,2",
            name = "NBA",
            playnum = 0,
            preview_url = "http://v9-xg-web-s.ixigua.com/c1c9629af013e80a234cdb26559cdc65/6256c83b/video/tos/cn/tos-cn-v-0000c001/af2f19d590434a7ca8bdc9f14de79f7f/?a=1768&br=1251&bt=1251&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&dr=0&ds=1&er=&ft=dcAui--bz7ThWbLjg1t8Z&l=2022041319551901015120",
            publish_time = "null",
            title = "这球真难防！NBA今日最佳过人：利拉德单打撤步三分",
            userid = "85383446500",
            verifycode = "0c8d8337f4b2f5b536d67be96914409c",
            videomainimag = "http://js2.a.yximgs.com/upic/2020/08/30/18/BMjAyMDA4MzAxODI5NTNfMTUwNzUyMzE0M18zNTI4NDY0NjM2MF8xXzM=_low_Bd92c09c4929315191905c7155a86fdf5.webp?tag=1-1599271260-vdg-0-nfaf5rlpwj-2f810cb65cedf886&bp=10000",
            videopath = "http://txmov2.a.yximgs.com/upic/2020/08/30/18/BMjAyMDA4MzAxODI5NTNfMTUwNzUyMzE0M18zNTI4NDY0NjM2MF8xXzM=_b_B691e24803172177e2603070153863997.mp4?tag=1-1599271260-vdg-1-1x8b9x7h8g-62335545041210d4&tt=b&bp=10000"
        )
    )

    private lateinit var tabAdapter: TabAdapter
    private lateinit var videoAdapter: VideoAdapter
    private lateinit var glideAdapter: GlideAdapter



    @SuppressLint("SuspiciousIndentation", "MissingInflatedId")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_home, container, false)

        // 获取铃铛图标和小红点视图
        val bellIcon = view.findViewById<ImageView>(R.id.bell_black)
        notificationBadge = view.findViewById(R.id.notification_badge) // 需要在XML中给TextView添加ID
        // 设置点击监听器

        bellIcon.setOnClickListener {
            hideNotificationBadge()
        }
        tabRecyclerView = view.findViewById(R.id.tabRecyclerView)
        // 确保 LinearLayoutManager 是水平方向
        tabRecyclerView.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        tabAdapter = TabAdapter(tabList) { position ->
            // 处理标签点击，更新选中状态
            tabList.forEachIndexed { index, tabItem ->
                tabList[index] = tabItem.copy(isSelected = index == position)

            }
            type = tabList[position].title
            tabAdapter.notifyDataSetChanged()
            // 根据选中的标签切换布局
            toggleLayoutBasedOnTab()

            // 根据选中的标签切换视频列表数据
            loadVideoData(position)

        }
        tabRecyclerView.adapter = tabAdapter

        // 初始化视频列表
        videoRecyclerView = view.findViewById(R.id.rv)   // ID为rv的RecyclerView
        videoRecyclerView.layoutManager = LinearLayoutManager(context)
        videoAdapter = VideoAdapter(videoList) { video ->
            // 处理视频点击事件
            // TODO: 跳转到视频详情页
//            val intent = Intent(context, VideoActivity::class.java)
//            startActivity(intent)

        }
        videoRecyclerView.adapter = videoAdapter

        glideRecyclerView = view.findViewById(R.id.rv2)
        glideRecyclerView.layoutManager = GridLayoutManager(context, 2)
        glideAdapter = GlideAdapter(videoList) { video ->
            // 处理视频点击事件
            // TODO: 跳转到视频详情页
//            val intent = Intent(context, VideoActivity::class.java)
//            startActivity(intent)
        }
        glideRecyclerView.adapter = glideAdapter

        // 初始布局状态
        toggleLayoutBasedOnTab()
        // 加载默认标签的视频数据
        loadVideoData(0)
        return view
    }
    /**
     * 根据当前选中的标签切换布局
     */
    private fun toggleLayoutBasedOnTab() {
        when (type) {
            "推荐" -> {
                videoRecyclerView.visibility = View.GONE
                glideRecyclerView.visibility = View.VISIBLE
            }
            else -> {
                videoRecyclerView.visibility = View.VISIBLE
                glideRecyclerView.visibility = View.GONE
            }
        }
    }
    private fun hideNotificationBadge() {
        notificationBadge?.visibility = View.GONE
    }

    private fun loadVideoData(tabPosition: Int) {
        // 根据标签位置确定labelId
        val labelId = when (tabPosition) {
            0 -> 1 // 关注
            1 -> 2 // 推荐
            2 -> 3 // Vlog
            3 -> 4 // 影视
            4 -> 5 // 娱乐
            5 -> 6 // 体育
            6 -> 7 // NBA
            else -> 1
        }
    }


}